var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import React, { useEffect, useState } from "react";
import { PickerView, PickerViewColumn, View } from "@tarojs/components";
/**
 * DatePicker 选择时间Picker
 */
var DatePicker = function (_a) {
    var _b = _a.mode, mode = _b === void 0 ? "date" : _b, _c = _a.minDate, minDate = _c === void 0 ? new Date("1900/01/01") : _c, _d = _a.maxDate, maxDate = _d === void 0 ? new Date("2100/01/01") : _d, _e = _a.defaultValue, defaultValue = _e === void 0 ? new Date() : _e, onChange = _a.onChange, columnStyle = _a.columnStyle, textStyle = _a.textStyle, activeTextStyle = _a.activeTextStyle, style = _a.style;
    // 设置picker默认植
    var _f = __read(useState([]), 2), pickerValue = _f[0], setPickerValue = _f[1];
    // 1 2 3列数据
    var _g = __read(useState([]), 2), pickerData = _g[0], setPickerData = _g[1];
    // 计算出数据源
    useEffect(function () {
        var pickerData = []; // 源数据
        // 1. 年 最小、最大
        var years = [];
        for (var year = minDate.getFullYear(); year <= maxDate.getFullYear(); year++) {
            years.push(year + "年");
        }
        pickerData.push(years);
        if (mode === "date" || mode === "date-year-month") {
            // 2. 月
            var months = [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ];
            pickerData.push(months);
        }
        if (mode === "date") {
            // 3. 日
            var days = [
                "1日",
                "2日",
                "3日",
                "4日",
                "5日",
                "6日",
                "7日",
                "8日",
                "9日",
                "10日",
                "11日",
                "12日",
                "13日",
                "14日",
                "15日",
                "16日",
                "17日",
                "18日",
                "19日",
                "20日",
                "21日",
                "22日",
                "23日",
                "24日",
                "25日",
                "26日",
                "27日",
                "28日",
                "29日",
                "30日",
                "31日"
            ];
            pickerData.push(days);
        }
        // 拿到有效日期
        var validDate = getValidDate(defaultValue.getFullYear() + "/" + (defaultValue.getMonth() +
            1) + "/" + defaultValue.getDate());
        // 计算数据源、日期所对应的下标索引
        var pickerValue = getPickerValue(pickerData, validDate);
        setPickerData(pickerData);
        setPickerValue(pickerValue);
        // 主动回调onChange
        onChange && onChange(validDate);
    }, []);
    // 输入:日期
    // 输出：范围内的有效日期 min <= date <= max && 日期合法[月->日] // 闰年/是否有31
    var getValidDate = function (date /** 'yyyy/mm/dd' */) {
        var validDate = new Date(date);
        // 无效日期
        if (!(validDate instanceof Date)) {
            return minDate;
        }
        if (mode === "date") {
            validDate = _getValidDate(date);
        }
        // 有效日期范围内
        if (validDate.getTime() < minDate.getTime()) {
            // 年
            if (validDate.getFullYear() < minDate.getFullYear()) {
                validDate.setFullYear(minDate.getFullYear());
            }
            // 月
            if (validDate.getTime() < minDate.getTime() &&
                validDate.getMonth() < minDate.getMonth()) {
                validDate.setMonth(minDate.getMonth());
            }
            // 日
            if (validDate.getTime() < minDate.getTime()) {
                validDate = new Date(minDate);
            }
        }
        if (validDate.getTime() > maxDate.getTime()) {
            // 年
            if (validDate.getFullYear() > maxDate.getFullYear()) {
                validDate.setFullYear(maxDate.getFullYear());
            }
            // 月
            if (validDate.getTime() > maxDate.getTime() &&
                validDate.getMonth() > maxDate.getMonth()) {
                validDate.setMonth(maxDate.getMonth());
            }
            // 日
            if (validDate.getTime() > maxDate.getTime()) {
                validDate = new Date(maxDate);
            }
        }
        return validDate;
    };
    // 根据年月日数据源、下标索引，计算出对组合后的日期
    var getChangeDate = function (data, indexs) {
        var date = [];
        for (var index = 0; index < data.length; index++) {
            var elements = data[index]; // 列数组
            var row = indexs[index]; // 列对应的行索引
            if (elements && elements[row]) {
                var s = elements[row];
                date.push(s
                    .replace(/年/g, "")
                    .replace(/月/g, "")
                    .replace(/日/g, ""));
            }
        }
        return date.join("/");
    };
    // 根据年月日数据源，计算出对应所在indexs
    var getPickerValue = function (data, date) {
        var indexs = [];
        var dateS = [
            date.getFullYear() + "\u5E74",
            date.getMonth() + 1 + "\u6708",
            date.getDate() + "\u65E5"
        ];
        var _loop_1 = function (index) {
            var elements = data[index]; // 列数组
            var ds = dateS[index];
            var i = elements.findIndex(function (e) { return e === ds; });
            indexs.push(i > -1 ? i : 0);
        };
        for (var index = 0; index < data.length; index++) {
            _loop_1(index);
        }
        return indexs;
    };
    // 获取合法的日期: 如果转换前后的年月日相等则为合法 28/29/30/31
    var _getValidDate = function (date /** 'yyyy/mm/dd' */) {
        var validDate = new Date(date);
        var dateS = validDate.getFullYear() + "/" + (validDate.getMonth() +
            1) + "/" + validDate.getDate();
        if (date !== dateS) {
            // 日期校正
            var dates = date.split("/");
            var day = getValidDay(Number(dates[0]), Number(dates[1]));
            validDate = new Date(dates[0] + "/" + dates[1] + "/" + day);
        }
        return validDate;
    };
    function getValidDay(year, month) {
        var dayT = 1;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                dayT = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                dayT = 30;
                break;
            case 2:
                dayT = year % 4 === 0 ? 29 : 28;
                break;
        }
        return dayT;
    }
    var onDateChange = function (e) {
        // 1. 拿到原始数组索引
        var indexs = e.detail.value;
        // 2. 根据索引拿到原始的年月日字符串
        var date = getChangeDate(pickerData, indexs);
        // 3. 拿到有效日期
        var validDate = getValidDate(date);
        // 4. 计算数据源、日期所对应的下标索引
        var pickerValueT = getPickerValue(pickerData, validDate);
        // 5. 更正索引值
        setPickerValue(pickerValueT);
        onChange && onChange(validDate);
    };
    var getStyle = function (index, col) {
        var active = index === pickerValue[col];
        if (active && activeTextStyle) {
            return activeTextStyle;
        }
        return __assign({ overflow: "hidden", whiteSpace: "nowrap", textOverflow: "ellipsis", height: 34 + "px", lineHeight: 34 + "px", fontWeight: active ? 600 : 400, color: active ? "#FF7242" : "#000000" }, textStyle);
    };
    return (React.createElement(PickerView, { indicatorStyle: "height: 34px;", style: __assign({ height: 430 + 'rpx', backgroundColor: "#ffffff" }, style), value: pickerValue, onChange: onDateChange }, pickerData && pickerData.length > 0
        ? pickerData.map(function (d, index) {
            return (React.createElement(PickerViewColumn, { key: index, style: __assign({ textAlign: "center" }, columnStyle) }, d.map(function (item, i) {
                return (React.createElement(View, { style: getStyle(i, index), key: i }, item));
            })));
        })
        : null));
};
export default DatePicker;
